package zero.tech.games.logic.data;

import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.alibaba.fastjson.JSONObject;

import zero.tech.games.common.RedisConsts;
import zero.tech.games.common.ThreadRunner;
import zero.tech.games.common.data.AIRoomNodeConfig;
import zero.tech.redis.cache.RedisUtil;
@Configuration
public class ServerTimer extends ThreadRunner{
	@Autowired
	private RedisUtil redisUtil;
	@Override
	public void process() {
		while(true) {
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			try {
				handle();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	private void handle() {
		boolean f=redisUtil.hasKey(RedisConsts.B_GM_OPEN_SWITCH);
		if(f) {
			if(ServerData.ALL_GAME_OPEN_SWITCH==1) {
				ServerData.ALL_GAME_OPEN_SWITCH=0;
			}
		}else {
			if(ServerData.ALL_GAME_OPEN_SWITCH==0) {
				ServerData.ALL_GAME_OPEN_SWITCH=1;
			}
		}
		Map<Object,Object> map=redisUtil.hgetAll(RedisConsts.B_GM_OPEN_SWITCH_BY_GAME);
		if(map!=null&&!map.isEmpty()) {
			for(Entry<Object,Object> entry:map.entrySet()) {
				int game_id=Integer.valueOf(entry.getKey().toString());
				boolean open=Boolean.valueOf(entry.getValue().toString());
				ServerData.changeSwitchByGameId(game_id,open);
			}
		}
		
		
		Map<Object,Object> map1=redisUtil.hgetAll(RedisConsts.B_GM_AI_ROOMNODE_MAP);
		if(map1!=null&&!map1.isEmpty()) {
			for(Entry<Object,Object> entry:map1.entrySet()) {
				String key=entry.getKey().toString();
				String str=entry.getValue().toString();
				AIRoomNodeConfig aiRoomNodeConfig = JSONObject.parseObject(str, AIRoomNodeConfig.class);
				ServerData.changeAiRoomNodeMap(key,aiRoomNodeConfig);
			}
		}
	}
	
	
}
